package com.cheyun.netsalev3.repository

import androidx.paging.PageKeyedDataSource
import com.cheyun.netsalev3.bean.DaoDian
import com.cheyun.netsalev3.bean.DaoDianParam
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.json.JSONObject

class HomeTaskDaoDianPageRepository : BasePageRepository<DaoDianParam>() {
    var stm= FunctionUtils.getTime2()
    override fun loadInitial(params: PageKeyedDataSource.LoadInitialParams<Int>, callback: (data: MutableList<DaoDianParam>) -> Unit) {
        ApiClient.getInstance().getArriveData(1, stm)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<DaoDian>>() {
                    override fun onSuccess(t: BaseResp<DaoDian>) {
                        t.data?.rows?.apply {
//                            t.data?.rows?.forEach { c-> c.is_leave = if(c.leavetime.equals("") || c.leavetime.equals("0"))0 else 1 }
                            total.postValue(t.data!!.total)
                            operData(this)
                            callback(this)
                        }

                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }


                })
    }

    override fun loadAfter(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, DaoDianParam>) {
        ApiClient.getInstance().getArriveData(params.key, stm)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<DaoDian>>() {
                    override fun onSuccess(t: BaseResp<DaoDian>) {
                        t.data?.rows?.apply {
//                            t.data?.rows?.forEach { c-> c.is_leave = if(c.leavetime.equals("") || c.leavetime.equals("0"))0 else 1 }
                            operData(this)
                            callback.onResult(this, params.key+1)
                        }
                    }

                    override fun onError(ex: ApiException) {
                    }


                })
    }

    override fun loadBefore(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, DaoDianParam>) {
        ApiClient.getInstance().getArriveData(params.key, stm)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<DaoDian>>() {
                    override fun onSuccess(t: BaseResp<DaoDian>) {
                        t.data?.rows?.apply {
//                            t.data?.rows?.forEach { c-> c.is_leave = if(c.leavetime.equals("") || c.leavetime.equals("0"))0 else 1 }
                            operData(this)
                            callback.onResult(this, params.key-1)
                        }
                    }

                    override fun onError(ex: ApiException) {
                    }


                })
    }

    /**
     * 分享
     */
    fun shareData(id: String, shareuid: String) {
        ApiClient.getInstance().getUpdate(id, shareuid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<JSONObject>>() {
                    override fun onSuccess(t: BaseResp<JSONObject>) {
                        dataSourceFactory.sourceLiveData.value?.invalidate()
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    fun operData(d:MutableList<DaoDianParam>){
        var showShare = false
        var loginData = MySharedPreferences.getLoginData()
        loginData?.member?.apps?.receive?.apply {
            for (i in this) {
                if(i == 5){
                    showShare = true
                }
            }
        }
        d?.forEach { c->
            c.enableShare = showShare && c.fid == 0
        }

    }
}